package org.itli.jnsocket.Mapper;

import org.apache.ibatis.annotations.*;
import org.itli.jnsocket.DTO.ProductDTO;
import org.itli.jnsocket.Pojo.Customer;
import org.itli.jnsocket.Pojo.Goods;

import java.math.BigInteger;
import java.util.List;

@Mapper
public interface OutboundMapper {

    //-------------------------------------向外销售的事务-------------------------------------------

    //查询客户
    @Select("select id,customer_no,customer_name,customer_address,customer_contact,customer_phone from jn_wms_customer where id=#{id}")
    Customer SelectCustomer(BigInteger id);

    //写入销售记录表
    @Insert("INSERT INTO jn_wms_sale_order (" +
            "    order_sn," +
            "    customer_id," +
            "    customer_no," +
            "    customer_name," +
            "    customer_address," +
            "    customer_contact," +
            "    customer_phone," +
            "    order_amount," +
            "    order_type," +
            "    order_date," +
            "    pay_type," +
            "    remark," +
            "    create_time," +
            "    create_by," +
            "    update_time," +
            "    update_by," +
            "    warehouse_id" +
            ") VALUES (" +
            "    #{order_sn}, " +
            "    #{customer.id},  " +
            "    #{customer.customer_no}, " +
            "    #{customer.customer_name}, " +
            "    #{customer.customer_address}, " +
            "    #{customer.customer_contact}, " +
            "    #{customer.customer_phone}, " +
            "    #{order_amount},      " +
            "    'S',          " +
            "    #{order_date}, " +
            "    'Y',        " +
            "    #{remark}, " +
            "    NOW(), " +
            "    #{create_by},   " +
            "    NOW(), " +
            "    #{update_by},    " +
            "    #{warehouse_id}       " +
            ");")
    int Insert_sale_order(Customer customer, double order_amount,String order_date,String pay_type,String update_by,String create_by,String order_sn,String remark,BigInteger warehouse_id );

    //从订单表查询出订单对应的 id
    @Select("select id from jn_wms_sale_order where order_sn = #{order_sn}")
    int SelectOrderID(String order_sn);

    //从商品表中取出 sn
    @Select("select goods_sn from jn_wms_goods where id = #{id}")
    String SelectGoodsSN(BigInteger id);

    //从商品表中取出规格
    @Select("select goods_spec from jn_wms_goods where id = #{id}")
    String SelectGoodsSec(BigInteger id);

    //写入商品表
    @Insert("INSERT INTO jn_wms_sale_order_goods (" +
            "    sale_id," +
            "    order_sn," +
            "    goods_id," +
            "    goods_sn," +
            "    goods_name," +
            "    goods_spec," +
            "    goods_price_out," +
            "    goods_count," +
            "    create_time," +
            "    create_by," +
            "    update_time," +
            "    update_by," +
            "    del_flag" +
            ") VALUES (" +
            "    #{sale_id}, " +
            "    #{order_sn}, " +
            "    #{goods.goods_id}, " +
            "    #{goods.goods_sn}, " +
            "    #{goods.goods_name}, " +
            "    #{goods.goods_spec}, " +
            "    #{goods.goods_price_out}, " +
            "    #{goods.goods_count}," +
            "    now(), " +
            "    #{create_by}, " +
            "    now()," +
            "    #{update_by}," +
            "    '0' " +
            ");")
    int Insert_sale_order_goods(int sale_id, String order_sn, ProductDTO goods, String create_by, String update_by);

    //查询出本商品的库存数据
    @Select("select * from jn_wms_stock where goods_id = #{goods_id} and warehouse_id = #{warehouse_id}")
    Goods SelectGoods_Stock(BigInteger goods_id,BigInteger warehouse_id);

    //更新商品库存数据
    @Update("update jn_wms_stock set goods_stock = #{goods_stock} where goods_id = #{goods_id} and warehouse_id = #{warehouse_id}")
    int UpdateGoods_Stock(BigInteger goods_id,BigInteger warehouse_id,int goods_stock);


    //------------------------------删除库存不足的记录------------------------------------
    @Delete(" delete from jn_wms_stock where goods_id = #{goods_id}")
    int Delete_Goods_Stock(BigInteger goods_id);
    //----------------------------------------------------------------------------------



    //--------------------------------------------------------------------------------------------

}
